package com.icinfo.cloud.provider.punish.common.publicsecurityentrust.dto;


import com.icinfo.cloud.provider.common.exception.BusinessException;
import lombok.Data;
import lombok.experimental.Accessors;
import org.hibernate.validator.constraints.Length;

/**
 * 公安委托文号保存配置
 *
 * @author HKxxx
 * @date 2024/1/2 11:00
 */
@Data
@Accessors(chain = true)
public class PublicSecurityDocumentNumberBaseDTO {

    /**
     * 号段开始
     */
    @Length(min = 9, max = 9, message = "开始号段数必须为九位数字")
    private String numberStart;

    /**
     * 号段结束
     */
    @Length(min = 9, max = 9, message = "结束号段数必须为九位数字")
    private String numberEnd;

    /**
     * 校验号段
     */
    public void verifyNumber() {
        long start = -1l;
        long end = -1l;
        try {
            start = Long.parseLong(numberEnd);
            end = Long.parseLong(numberStart);
        } catch (Exception e) {
            throw new BusinessException("开始号段数/结束号段数 必须为数字！");
        }
        if (start < 0 || end < 0) {
            throw new BusinessException("开始号段数/结束号段数 必须为正整数！");
        }
    }

    /**
     * 获取号段号数
     *
     * @return
     */
    public Integer getNumberSegment() {
        verifyNumber();
        Long end = Long.parseLong(this.numberEnd);
        Long start = Long.parseLong(this.numberStart);
        long numberSegment = end - start + 1;
        if (numberSegment < 1) {
            throw new BusinessException("结束号段不能小于开始号段");
        }
        return (int) numberSegment;
    }

}
